﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using Sunny.UI;
using CoreShop.Model;
using FastMember;
using CoreShop.Services;
using CoreShop.Model.FromBody;
using CoreShop.Model.UI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using CoreShop.App.Units.Interface;

namespace CoreShop.App
{
    public partial class frmCouponUsage : UIForm
    {
        public corecmsuser member = null;
        public VipCouponInfoModel foodcouponuser = null;
        private List<corecmsfoodordering> orderingDetail;
        public List<VipCouponInfoModel> couponPays = null;//支付的券明细
        private List<VipCouponInfoModel> couponSelecteds = null;//已选择支付的券明细
        public frmCouponUsage(List<corecmsfoodordering> _orderingDetail, corecmsuser _member
            , List<VipCouponInfoModel> _couponSelecteds)
        {
            orderingDetail = _orderingDetail;
            member = _member;
            this.couponSelecteds = _couponSelecteds;
            InitializeComponent();
            txtNumber.TextBox.GotFocus += txtNumber_GotFocus;
        }

        private void txtNumber_GotFocus(object sender, System.EventArgs e)
        {
            numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.String, 0);
        }

        private void txtKey_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                numPad_OKButtonClick(numPad, txtNumber.Text,0,0);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void txtSendKey_Validating(object sender, CancelEventArgs e)
        {

        }

        private void frmInputTxt_Shown(object sender, EventArgs e)
        {
            txtNumber.Focus();
        }

        private async void numPad_OKButtonClick(object sender, string text, int intvalue, double doublevalue)
        {
            if (string.IsNullOrEmpty(txtNumber.Text))
            {
                UIMessageBox.Show("输入的内容不能为空！");
                return;
            }
            long userId = 0;
            if (member != null) userId = member.id;
            string msg = "";
            var posRemoteServices = new PosRemoteServices(AppConstVars.sysUser.ccessToken);
            var orderCoupon = new OrderCouponPost();
            orderCoupon.storeId = AppConstVars.sysUser.storeId;
            orderCoupon.couponCode = txtNumber.Text;
            orderCoupon.userId = userId;
            orderCoupon.wxUserId = 0;
            orderCoupon.channel = 1;
            string orderStr = JsonConvert.SerializeObject(orderingDetail);
            List<OrderCouponItem> orderItems = JsonConvert.DeserializeObject<List<OrderCouponItem>>(orderStr);
            orderCoupon.items = orderItems;
            var couponUser = await MemberApiUtil.GetCouponByOrder(orderCoupon, AppConstVars.sysUser.ccessToken);
            if (couponUser == null) 
            {
                UIMessageBox.Show(msg);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
